<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <view class="jtop_l" @tap="navigateBack"
        ><image src="../../static/img/ic_back.png"></image
      ></view>
      <view class="jtop_c">邮箱</view>
      <!-- <view class="jtop_r">确定</view> -->
    </view>
    <view class="tophr"></view>

    <view class="ueb">
      <input type="" placeholder="无" v-model="email" />
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState, mapMutations } from 'vuex';

export default {
  data() {
    return {
      email: '',
    };
  },
  onShow() {
    this.email = uni.getStorageSync('user').userInfoResponse.email;
    console.log(this.email);
  },
  computed: {
    ...mapState(['hasLogin', 'forcedLogin']),
  },
  methods: {
    ...mapMutations(['logout']),
    bindLogin() {
      uni.navigateTo({
        url: '../login/login',
      });
    },
    bindLogout() {
      this.logout();
      /**
       * 如果需要强制登录跳转回登录页面
       */
      if (this.forcedLogin) {
        uni.reLaunch({
          url: '../login/login',
        });
      }
    },
    navigateBack() {
      var email = this.email;

      //对象的方式创建
      var reg = new RegExp(/^([a-zA-Z\d])(\w|\-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/);
      console.log(reg.test(email));
      if (email.length > 0 && !reg.test(email)) {
        this.repeat_click = true;
        uni.showToast({
          icon: 'none',
          title: '邮箱格式错误',
        });
        return;
      }

      request({
        url: '/ucenter/user/modify',
        data: {
          email: email,
        },

        method: 'PUT',
        success: openIdRes => {
          console.log(openIdRes);

          // 已经授权了、查询到用户的数据了
          if (openIdRes.data.success == true) {
            var aa = uni.getStorageSync('user');
            aa.userInfoResponse['email'] = email;
            uni.setStorageSync('user', aa);

            // 					this.repeat_click = true;
            //uni.showToast({
            // 	title: '修改成功'
            // })

            uni.navigateBack();
          } else {
            // 错误
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },
  },
};
</script>

<style>
.ueb {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  margin: 20rpx 0 0 0;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  background: #ffffff;
}
.ueb text {
  width: auto;
  height: 42rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  line-height: 42rpx;
  text-align: right;
  color: #000000;
  margin: 24rpx 0rpx 24rpx 30rpx;
}
.ueb input {
  width: 530rpx;
  height: 42rpx;
  display: block;
  float: left;
  margin: 20rpx 30rpx 20rpx 30rpx;
  font-size: 34rpx;
  line-height: 42rpx;
  color: #000000;
}
.ueb_ts {
  width: auto;
  height: 42rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  line-height: 42rpx;
  text-align: right;
  color: #888888;
  margin: 24rpx 0rpx 24rpx 30rpx;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}
.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}
.jtop_lt {
  margin: 0 0 0 10rpx;
}
.jtop_r {
  width: auto;
  height: 88rpx;
  display: block;
  float: right;
  margin: 0rpx 20rpx 0 0;
  line-height: 88rpx;
  font-size: 32rpx;
  color: #000000;
  color: #256aff;
}
.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.tophr {
  width: 100%;
  height: 88rpx;
}
</style>
